#include "StdAfx.h"
#include "IResize.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IResize::FunctionNameIResize = "ScaleResize";

IResize::IResize(void) 
{
	_fnApi.SetFunctionName(IResize::FunctionNameIResize);
	_fnApi.SetFunctionDescription("This fn scales a picture");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefDouble(0, "ScaleFactor", "sets scale factor for image, min=0, max=10", ProcParamDef::input, 0, 10 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IResize::~IResize(void)
{

}


bool IResize::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamDouble *factor = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(0));	//Check the Param number
		//ProcParamInt *result = dynamic_cast<ProcParamInt *>(step->GetOutputs()->GetParam(0));		//Check the type and check the DefType
		if  (factor != '\0')
		{
			cv::Mat outp;
			cv::Mat inp = cv::imread(step->GetIntermediateImagePathFilename());

			//_initialW = inp.		//getwidth
			if (factor >= 0 )
				cv::resize(inp, outp, cv::Size(), factor->GetValue(), factor->GetValue());
			else
				cv::resize(inp, outp, cv::Size(), 1/(factor->GetValue()), 1/(factor->GetValue()));

			cv::imwrite(step->GetIntermediateImagePathFilename(), outp);

			inp.release();
			//result->SetValue(0);
			rc = true;
		}
	}
	return rc;
}
